package com.demo.datastructure.binarySearch;

/**
 * @description:  二分法 找到重复元素中的第一个元素
 * @author: ljr
 * @time: 2021/5/11 21:05
 */
public class BinarySearch4 {

    public static int bsearch(int[] a, int n, int value) {
        int low = 0;
        int high = n-1;
        while(low <= high){
            int mid = low+(high-low >> 1);
            if(a[mid] >= value){
                high = mid-1;
            }else{
                low = mid+1;
            }
        }

        if(low < n && a[low] == value){
            return low;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,4,5,6,8,8,8,11,18};
        System.out.print(BinarySearch4.bsearch(nums,nums.length,8));
    }

}
